<?php
if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

/**
 * 导入CSS
 * @param string $css CSS路径
 * @return string
 */
function load_css( $css )
{
    return "<link rel=\"stylesheet\" href=\"" . base_url() . $css . "\" />";
}

/**
 * 导入JS
 * @param string $js JS路径
 * @return string
 */
function load_js( $js )
{
    return '<script type="text/javascript" language="javascript" src="' . base_url() . $js . '"></script>';
}

/**
 * 构造url函数
 * @param string $controller 控制器
 * @param string $method 方法
 * @param mixed $params 参数
 * @param boole $output 是否输出（默认是）
 * @return string
 */
function my_url( $controller, $method = '', $params = '', $output = true )
{
    $uri = $controller;
    if ( $method ) $uri .= '/' . $method;
    if ( $params ) $uri .= '/' . $params;
    $url = base_url( $uri );
    if ( $output )
    {
        echo $url;
    }
    else
    {
        return $url;
    }
}

/**
 * 创建令牌
 * @return string
 */
function create_token()
{
    $CI = & get_instance();
    $time = time();
    $CI->session->set_userdata( 'token', $time );
    return form_hidden( 'token', md5( $time ) );
}

/**
 * 检查是否具有某操作权限
 * @param string $permission
 * @return bool
 */
function chk_permission( $permission )
{
    $CI = & get_instance();
    return $CI->permission->chkPermission( $permission );
}

function init_kindeditor( $name, $width = 600, $height = 220, $lang = 'zh_CN' )
{
    static $loaded;
    if ( !$loaded )
    {
        echo load_js( 'js/kindeditor/kindeditor-min.js' );
        echo load_js( "js/kindeditor/lang/{$lang}.js" );
        $loaded = true;
    }
    ?>
    <script type="text/javascript">
        var editor;
        KindEditor.ready(function(K) {
            editor = K.create('textarea[name="<?php echo $name; ?>"]', {
                resizeType : 1,
                allowPreviewEmoticons : false,
                allowImageUpload : false,
                items : [
                    'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
                    'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                    'insertunorderedlist'],
                height : '<?php echo $height; ?>',
                width : '<?php echo $width; ?>'
            });
        });
    </script>
    <?php
}

/**
 * 获取上传文件配置数据
 */
function get_upload_config()
{
    $config = array(
        'upload_path' => config_item( 'upload_path' ),
        'allowed_types' => config_item( 'allowed_types' ),
        'max_size' => config_item( 'max_size' ),
        'encrypt_name' => config_item( 'encrypt_name' ),
    );
    return $config;
}
/**
 * 自定义时间格式化
 * @param string $timestamp 时间戳
 * @param string $format 时间格式化
 * @return string
 */
function my_dateformat( $timestamp, $format = '' )
{
    $now = time();
    if ( $format ) return date($format, $timestamp);
    if ( $timestamp > $now ) return '未来…';
    $diff = $now - $timestamp;
    if ( $diff < 60 )
    {
        return "{$diff}秒前";
    }
    else if( $diff >= 60 && $diff < 3600 )
    {
        return intval($diff/60) . '分钟前';
    }
    else if( $diff >= 3600 && $diff < 24*3600 )
    {
        return intval($diff/3600) . '小时前';
    }
    else if( $diff >= 24*3600 && $diff < 24*3600*30 )
    {
        return intval($diff/24/3600) . '天前';
    }
    else if( $diff >= 24*3600*30 && $diff < 24*3600*30*12 )
    {
        return intval($diff/24/3600/30) . '月前';
    }
    else
    {
        return intval($diff/24/3600/30/12) . '年前';
    }
}
?>
